/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.http.server.handler.ext;

import hxg.http.server.NamedRequestHandler;
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.nio.protocol.BasicAsyncResponseProducer;
import org.apache.http.nio.protocol.HttpAsyncExchange;
import org.apache.http.protocol.HttpContext;

/**
 *
 * @author hstel-hxg
 */
public class HttpHtmlHandler extends NamedRequestHandler {

    @Override
    public void handle(HttpRequest request, HttpAsyncExchange httpexchange, HttpContext context) throws HttpException, IOException {
        HttpResponse response = httpexchange.getResponse();
        handleInternal(request, response, context);
        httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
    }

    private void handleInternal(HttpRequest request, HttpResponse response, HttpContext context) {
        response.setStatusCode(HttpStatus.SC_OK);
        Header[] headers=request.getAllHeaders();
        for(Header header:headers){
            System.out.println(header.getName()+":"+header.getValue());
        }
        System.out.println(context.getAttribute("test"));
        NStringEntity entity = new NStringEntity(
                "<html><body><h1>Test</h1></body></html>",
                ContentType.create("text/html", "UTF-8"));
        response.setEntity(entity);
        System.out.println(response);
    }

    @Override
    public String getName() {
        return "TestHandler";
    }

}
